package javacompiler.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

import javacompiler.lex.JavaLexicalAnalyser;
import javacompiler.syntax.JavaSyntaxAnalyser;

public class JavaCompiler {
	
	/**
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		if (!checkArguments(args)) {
			System.err.println("Argumentos inválidos.");
			System.err.println("Uso correto: JavaCompiler nome-do-arquivo");
		} else {
			executeParser(args);
		}
	}

	private static void executeParser(String[] args)
			throws FileNotFoundException {
		File file = new File(args[0]);
		InputStreamReader stream = new InputStreamReader(new FileInputStream(file));
		JavaSyntaxAnalyser p = new JavaSyntaxAnalyser(new JavaLexicalAnalyser(stream));
		
		try {
			p.parse();
			System.out.println("The program is well formed.");
		} catch (Throwable e) {
			System.err.println("The program has an error.");
		}
	}

	private static boolean checkArguments(String[] args) {
		return args.length == 1;
	}
}
